<template>
  <el-card class="subject-page">
    <div class="header">
      <div class="search">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="学科名称">
            <el-input v-model="formInline.subjectName"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="formInline.subjectName = ''">清空</el-button>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="add">
        <el-button type="success" class="el-icon-edit" @click="add"
          >新增用户</el-button
        >
      </div>
    </div>
    <el-alert
      :title="`共${counts}条记录`"
      type="info"
      show-icon
      :closable="false"
    >
    </el-alert>
    <el-table
      :header-cell-style="{ background: '#FAFAFA' }"
      :data="subjectData"
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="id" label="序号" width="80"> </el-table-column>
      <el-table-column prop="subjectName" label="学科名称"> </el-table-column>
      <el-table-column prop="username" label="创建者"> </el-table-column>
      <el-table-column prop="addDate" label="创建日期" :formatter="dateFormat">
      </el-table-column>
      <el-table-column
        prop="isFrontDisplay"
        label="前台是否显示"
        :formatter="isFrontFormat"
      >
      </el-table-column>
      <el-table-column prop="twoLevelDirectory" label="二级目录">
      </el-table-column>
      <el-table-column prop="tags" label="标签"> </el-table-column>
      <el-table-column prop="totals" label="题目数量"> </el-table-column>

      <el-table-column label="操作" align="center" width="220">
        <template #default="scope">
          <el-button
            size="small"
            type="text"
            @click="
              $router.push(
                `directorys?id=${scope.row.id}&name=${scope.row.subjectName}`
              )
            "
            >学科分类</el-button
          >
          <el-button
            size="small"
            type="text"
            @click="
              $router.push(
                `tags?id=${scope.row.id}&name=${scope.row.subjectName}`
              )
            "
            >学科标签</el-button
          >
          <el-button type="text" @click="edit(scope.row.id)">修改</el-button>
          <el-button type="text" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next,sizes,jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="params.page"
        :page-sizes="[10, 20, 30, 50]"
        :total="counts"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form
        ref="formRef"
        :model="subjectForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="subjectForm.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="subjectForm.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { list, add, detail, update, remove, simpleSubject } from '@/api/hmmm/subjects'

export default {
  data () {
    return {
      formInline: {
        subjectName: ''
      },
      params: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      title: '',
      subjectForm: {
        subjectName: '',
        isFrontDisplay: true
      },
      subjectData: [],
      counts: null,

      dialogVisible: false,

      subjectOption: [],
      rules: {
        subjectName: [
          { required: true, message: '学科名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.list()
    this.simpleSubject()
  },
  methods: {
    onSubmit () {
      this.params.subjectName = this.formInline.subjectName

      this.list()
    },
    async list () {
      const { data } = await list(this.params)

      this.subjectData = data.items
      this.counts = data.counts
    },
    dateFormat (row) {
      return this.$dayjs(row.addDate).format('YYYY-MM-DD')
    },
    isFrontFormat (row) {
      if (row.isFrontDisplay === 1) {
        return '是'
      } else if (row.isFrontDisplay === 0) {
        return '否'
      }
    },
    async simpleSubject () {
      const { data } = await simpleSubject()

      this.subjectOption = data
    },
    handleSizeChange (val) {
      this.params.pagesize = val
      this.list()
    },
    handleCurrentChange (current) {
      this.params.page = current
      this.list()
    },
    async add () {
      this.title = '新增学科'
      this.dialogVisible = true
      await this.$refs.formRef.resetField()
    },
    async edit (id) {
      this.title = '修改学科'
      this.dialogVisible = true
      this.isAdd = false
      this.isEdit = true
      const { data } = await detail(id)

      this.subjectForm.id = id
      this.subjectForm.subjectName = data.subjectName
      this.subjectForm.isFrontDisplay = data.isFrontDisplay
    },

    async save () {
      try {
        if (this.title === '新增学科') {
          await this.$refs.formRef.validate()
          const res = await add(this.subjectForm)

          if (res.status === 200) {
            this.$message.success('添加成功')
          } else {
            this.$message.error('添加失败')
          }
        } else {
          const res = await update(this.subjectForm)
          if (res.status === 200) {
            this.$message.success('修改成功')
          } else {
            this.$message.error('修改失败')
          }
        }
        this.dialogVisible = false
        this.list()
      } catch (err) {
        this.$message.error(err.response.data)
      }
    },
    async del (id) {
      await this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(id)
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
      this.list()
    }
  }
}
</script>

<style scoped lang="scss">
.subject-page {
  margin: 20px 20px;
  .header {
    display: flex;
    justify-content: space-between;
  }
  .pagination {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
  }
  .el-dialog {
    .dialog-footer {
      display: block;
      text-align: center;
    }
  }
}
</style>
